package com.giantlizard.cloud.system.controller;

import com.giantlizard.cloud.base.generics.R;
import com.giantlizard.cloud.system.entity.SysDict;
import com.giantlizard.cloud.system.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统-字典 控制器
 */
@RestController
@RequestMapping("/dict")
@Api(value = "系统-字典", tags = {"系统-字典"})
public class SysDictController {

    private final SysDictService sysDictService;

    public SysDictController(SysDictService sysDictService) {
        this.sysDictService = sysDictService;
    }

    @ApiOperation(value = "获取所有字典")
    @GetMapping("/list")
    public R<List<SysDict>> list() {
        return R.ok(sysDictService.list());
    }

    @ApiOperation(value = "根据ID获取字典")
    @GetMapping("/{id}")
    public R<SysDict> getById(@ApiParam(name = "id", required = true, value = "字典ID") @PathVariable String id) {
        return R.ok(sysDictService.getById(id));
    }

    @ApiOperation(value = "新增字典")
    @PostMapping
    public R<Boolean> add(@RequestBody SysDict sysDict) {
        sysDictService.save(sysDict);
        sysDictService.refreshDict(sysDict.getDictName());
        return R.ok();
    }

    @ApiOperation(value = "更新字典")
    @PutMapping
    public R<Boolean> update(@RequestBody SysDict sysDict) {
        sysDictService.updateDict(sysDict);
        return R.ok();
    }

    @ApiOperation(value = "删除字典")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@ApiParam(name = "id", required = true, value = "字典ID") @PathVariable String id) {
        return R.ok(sysDictService.removeById(id));
    }

    @ApiOperation(value = "刷新字典")
    @PutMapping("/refresh")
    public R<Boolean> refresh() {
        sysDictService.refreshAllDict();
        return R.ok();
    }
} 